package cn.new7as.cas5s.ui.record;

import static cn.new7as.cas5s.tool.util.Utils.setLinerarHeight;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lxj.xpopup.util.XPopupUtils;

import java.util.List;

import cn.new7as.cas5s.R;
import cn.new7as.cas5s.tool.util.Utils;
import cn.new7as.cas5s.ui.easy.netapi.RecordsApi;

public class Recordsadapter extends BaseAdapter {

    private List<RecordsApi.Data> list;
    private Context context;


    public Recordsadapter(List<RecordsApi.Data> list, Context context) {
        super();

        this.context = context;
        this.list = list;

    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.layout_grid_records, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.repli = (TextView) convertView.findViewById(R.id.repli);
            viewHolder.sendcontent = (TextView) convertView.findViewById(R.id.sendcontent);
            viewHolder.sendtime = (TextView) convertView.findViewById(R.id.sendtime);

            viewHolder.details = (LinearLayout) convertView.findViewById(R.id.details);
            viewHolder.lrecords = (LinearLayout) convertView.findViewById(R.id.lrecords);

            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.sendcontent.setText(list.get(position).content);
        if (list.get(position).replyStatus == 0) {
            viewHolder.repli.setText("Processsing");
            viewHolder.repli.setTextColor(Color.parseColor("#19c656"));
        } else {
            viewHolder.repli.setText("Replied");
            viewHolder.repli.setTextColor(Color.parseColor("#0a0c06"));
        }

        viewHolder.sendtime.setText(list.get(position).feedbackDate.substring(0, 10));

        viewHolder.sendcontent.measure(0, 0);
        int height = viewHolder.sendcontent.getMeasuredHeight();
        setLinerarHeight(viewHolder.lrecords, height + XPopupUtils.dp2px(context, 125));

        viewHolder.details.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Utils.goextra(context, RecordsDetailActivity.class, "" + list.get(position).id);
            }
        });

        return convertView;
    }

    class ViewHolder {
        TextView repli;
        TextView sendcontent;

        TextView sendtime;


        LinearLayout details;
        LinearLayout lrecords;
    }
}
